package org.lq.us.web;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

/**
 * Servlet implementation class PhoneCodeServlet
 */
@WebServlet("/PhoneCodeServlet")
public class PhoneCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PhoneCodeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("image/png");
		QRCodeWriter codeWriter = new QRCodeWriter();
		String url = "";
		try {
			//System.out.println("sesio - code :"+request.getSession().getId());
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			request.getSession().setAttribute("uuid", uuid);
			BitMatrix bitMatrix = codeWriter.encode("http://ypl9421.gicp.net/hnkj_user/phone.html?uuid="+uuid, BarcodeFormat.QR_CODE, 350, 350);
			MatrixToImageWriter.writeToStream(bitMatrix, "png", response.getOutputStream());
		} catch (WriterException e) {
			e.printStackTrace();
		}
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
